package jxau.jwgl.utils.net;

import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.Response;

import java.io.IOException;
import java.net.CookieManager;
import java.net.CookiePolicy;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * OkHttpBuilder
 *
 * @author LongShu 2017/04/03
 */
public class OkHttpBuilder {

    // 连接超时时间
    private int connectTimeout = 60;
    // User-Agent
    private UserAgentInterceptor agentInterceptor;

    // Cookie管理,接受所有Cookie
    private static final CookieManager cookieManager = new CookieManager(null, CookiePolicy.ACCEPT_ALL);

    /**
     * 全局的拦截器
     */
    private static final List<Interceptor> globalInters = new ArrayList<>(2);

    /**
     * 添加UserAgent
     */
    public static abstract class UserAgentInterceptor implements Interceptor {
        public static final String HEADER = "User-Agent";

        @Override
        public Response intercept(Chain chain) throws IOException {
            Request request = chain.request();
            request = request.newBuilder().addHeader(HEADER, getUserAgent()).build();
            return chain.proceed(request);
        }

        protected abstract String getUserAgent();
    }

    /**
     * 创建ClientBuilder
     *
     * @param withCookie 是否包含Cookie
     */
    public OkHttpClient.Builder builder(boolean withCookie) {
        OkHttpClient.Builder httpBuiler = new OkHttpClient.Builder()
                .connectTimeout(connectTimeout, TimeUnit.SECONDS)
                .readTimeout(connectTimeout, TimeUnit.SECONDS);

        if (agentInterceptor != null) {
            httpBuiler.addInterceptor(agentInterceptor);
        }

        // 全局拦截器
        for (Interceptor inter : globalInters) {
            httpBuiler.addInterceptor(inter);
        }

        if (withCookie) {
            httpBuiler.cookieJar(new okhttp3.JavaNetCookieJar(cookieManager)).build();
        }
        return httpBuiler;
    }

    /**
     * 创建Client
     *
     * @param withCookie 是否包含Cookie
     */
    public static OkHttpClient client(boolean withCookie) {
        return new OkHttpBuilder().builder(withCookie).build();
    }

    /**
     * 添加全局的拦截器
     *
     * @param interceptor 拦截器
     * @return 是否添加成功
     */
    public static boolean addGlobalInterceptor(Interceptor interceptor) {
        synchronized (globalInters) {
            if (globalInters.contains(interceptor)) {
                return false;
            }
            return globalInters.add(interceptor);
        }
    }

    public int getConnectTimeout() {
        return connectTimeout;
    }

    public OkHttpBuilder setConnectTimeout(int connectTimeout) {
        this.connectTimeout = connectTimeout;
        return this;
    }

    public UserAgentInterceptor getAgentInterceptor() {
        return agentInterceptor;
    }

    public OkHttpBuilder setAgentInterceptor(UserAgentInterceptor agentInterceptor) {
        this.agentInterceptor = agentInterceptor;
        return this;
    }

    public static CookieManager getCookieManager() {
        return cookieManager;
    }

    public static List<Interceptor> getGlobalInters() {
        return globalInters;
    }
}
